#include <iostream>
#include "contacts.pb.h"

int main()
{
    std::string people_str;

    {
        contacts::PeopleInfo people;
        people.set_name("张三");  // 设置姓名
        people.set_age(20);       // 设置年龄
        // 将people序列化成string类型，存放到people_str中
        if(!people.SerializeToString(&people_str))  // 序列化成string
        {
            std::cerr << "序列化错误！" << std::endl;
            return -1;
        }
        std::cout << "序列化成功！结果：" << people_str << std::endl;
    }

    {
        contacts::PeopleInfo people;
        // 将people从string类型中反序列化到people_str中
        if(!people.ParseFromString(people_str))
        {
            std::cerr << "反序列化错误！" << std::endl;
            return -1;
        }
        std::cout << "反序列化成功！" << people_str << std::endl;
        std::cout << "姓名：" << people.name() << std::endl;
        std::cout << "年龄：" << people.age() << std::endl;
    }
   return 0;
}